import axios from '../../../utils/http'
import XLSX from 'xlsx'
import Cookies from 'js-cookie'
import {baseURL} from '../../../utils/http'

// 查询学期
export async function getTermList() {
  const {
    data
  } = await axios.get('/bac/term/getTermList')
  return data
}


// 查询周次 /term/weekList/ /iseInspection/analysisAll
export async function getWeekList(parameter) {
  const {
    data
  } = await axios.get('/bac/weekDate/getWeekListByTerm?' + parameter)
  return data
}


// 获取晚归表
export async function getReturnList(parameter) {
  const {
    data
  } = await axios.get('/dormNight/deadlineBack' + parameter)
  return data
}


// 添加 晚归 /iseSchedule/schedule
export async function addDutyDetails(stuName,stuNumber,backTime) {
  const {
    data
  } = await axios.post('/dormNight/deadlineBack', {
    stuName:stuName,
    stuNumber:stuNumber,
    backTime:backTime
  })
  return data
}

// 修改 值班情况  /iseSchedule/schedule
export async function updataDutyDetails(id , backTime) {
  const {
    data
  } = await axios.put('/dormNight/deadlineBack', {
    id: id,
    backTime: backTime
  })
  return data
}

// 删除晚归记录
export async function deleteDutyDetails(scheduleIds) {
  const {
    data
  } = await axios.delete('/dormNight/deadlineBack', {
    data: scheduleIds
  })
  return data
}

// Excel导入数据处理
export function excel(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsArrayBuffer(file)
    reader.onerror = e => {
      reject(e)
    }
    reader.onload = e => {
      const excelData = e.target.result
      const wb = XLSX.read(excelData, {
        type: 'array'
      })
      const firstSheetName = wb.SheetNames[0]
      const worksheet = wb.Sheets[firstSheetName]
      const columns = processHeader(worksheet)
      const tableData = XLSX.utils.sheet_to_json(columns)

      // if (!tableData[0].学期 && !tableData[0].周次) {
      //   resolve(false)
      // }
      resolve(tableData)
    }
  })


}

// 格式化Excel数据 表头
function processHeader(sheet) {
  const range = XLSX.utils.decode_range(sheet['!ref'])
  const C = range.e.c
  for (let i = 0; i <= C; i++) {
    sheet[String.fromCharCode(65 + i) + 1] = sheet[String.fromCharCode(65 + i) + 2]
    delete sheet[String.fromCharCode(65 + i) + 2]
  }
  return sheet
}

// 添加导入晚归表Excel数据
export async function addExceldata(excelData) {
  const {
    data
  } = await axios.post('/dormNight/deadlineBack/addPlanList', excelData)
  return data
}

// 导出Excel数据 /dormNight/deadlineBack/download     Excel数据导出
export function getExcel() {
  fetch(baseURL+'/dormNight/deadlineBack/download', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
//getselfExcel自习表的导出
export function getselfExcel() {
  fetch(baseURL+'study/download', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
// downloadShuttering
export function downloadShuttering() {
  fetch(baseURL+'study/downloadTemplate', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloads(blobUrl)
    })
}
export function downloadWorkShuttering() {
  fetch(baseURL+'/dormNight/deadlineBack/downLoadTemplate', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloadWork(blobUrl)
    })
}

function download(blobUrl) {
  const a = document.createElement('a')
  a.download = '晚归管理表'  + '.xlsx'

  a.href = blobUrl
  a.click()
}

function downloads(blobUrl) {
  const a = document.createElement('a')
  a.download = '自习安排表模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}

function downloadWork(blobUrl) {
  const a = document.createElement('a')
  a.download = '晚归管理表模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}
